package com.shinhye.elephantsafe;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shinhye.elephantsafe.db.dao.ApplockDao;
import com.shinhye.elephantsafe.domain.AppInfo;
import com.shinhye.elephantsafe.engine.AppInfomations;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class AppLockActivity extends Activity implements OnClickListener{
	private final String TAG = "AppLockActivity";
	private final int MSG_UNLOCK = 0x00;
	private final int MSG_LOCK = 0x01;
	
	private TextView tv_unlock;
	private TextView tv_locked;
	private TextView tv_applock_subtitle;
	private ApplockDao dao;
	private ArrayList<AppInfo> unlockAppList;
	private ArrayList<AppInfo> lockedAppList;
	private ListView lv_applock;
	private LinearLayout ll_loading;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			ll_loading.setVisibility(View.INVISIBLE);
			tv_applock_subtitle.setVisibility(View.VISIBLE);
			switch(msg.what) {
			case MSG_UNLOCK:
				if(unlockAppList.size() == 0)
					tv_no_lockapp.setVisibility(View.VISIBLE);
				else
					tv_no_lockapp.setVisibility(View.INVISIBLE);
				tv_applock_subtitle.setText("未加锁软件" + unlockAppList.size() + "个");
				lv_applock.setAdapter(new MyAdapter(true));
				break;
			case MSG_LOCK:
				if(lockedAppList.size() == 0)
					tv_no_lockapp.setVisibility(View.VISIBLE);
				else
					tv_no_lockapp.setVisibility(View.INVISIBLE);
				tv_applock_subtitle.setText("已加锁软件" + lockedAppList.size() + "个");
				lv_applock.setAdapter(new MyAdapter(false));
				break;
			}
		}
	};
	private TextView tv_no_lockapp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_applock);
		dao = new ApplockDao(this);
		
		tv_locked = (TextView) findViewById(R.id.tv_locked);
		tv_unlock = (TextView) findViewById(R.id.tv_unlock);
		tv_applock_subtitle = (TextView) findViewById(R.id.tv_applock_subtitle);
		lv_applock = (ListView) findViewById(R.id.lv_applock);
		ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
		tv_no_lockapp = (TextView) findViewById(R.id.tv_no_lockapp);
		
		tv_locked.setOnClickListener(this);
		tv_unlock.setOnClickListener(this);
		
		unlockAppList = new ArrayList<AppInfo>();
		lockedAppList = new ArrayList<AppInfo>();
		getData();
	}

	/**
	 * Start a new thread to update UI while loading a lot of data.
	 */
	private void getData() {
		ll_loading.setVisibility(View.VISIBLE);
		tv_applock_subtitle.setVisibility(View.INVISIBLE);
		new Thread(new Runnable() {
			@Override
			public void run() {
				List<AppInfo> infos = AppInfomations.getLaunchableAppInfos(AppLockActivity.this);
				for (AppInfo info : infos) {
					if(dao.find(info.getPackName())) {
						lockedAppList.add(info);
						Collections.sort(lockedAppList);
					} else {
						unlockAppList.add(info);
						Collections.sort(unlockAppList);
					}
				}
				handler.sendEmptyMessage(MSG_UNLOCK);
			}
			
		}).start();
	}
	
	/**
	 * A adapter to provider data for ListView.
	 * @author ShinHye
	 *
	 */
	class MyAdapter extends BaseAdapter {
		private boolean unlockflag; //true - unlock; false - lock
		
		public MyAdapter(boolean flag) {
			this.unlockflag = flag;
		}
		
		@Override
		public int getCount() {
			if(unlockflag)
				return unlockAppList.size();
			else
				return lockedAppList.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final View view;
			ViewHolder holder;
			if(convertView != null) {
				Log.i(TAG, "历史遗留物");
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				Log.i(TAG, "新大陆！");
				view = View.inflate(AppLockActivity.this, R.layout.item_applock_list, null);
				holder = new ViewHolder();
				holder.iv_appicon = (ImageView) view.findViewById(R.id.iv_appicon);
				holder.tv_appname = (TextView) view.findViewById(R.id.tv_appname);
				holder.iv_lock = (ImageView) view.findViewById(R.id.iv_lock);
				view.setTag(holder);
			}
			
			final AppInfo info;
			if(unlockflag) {
				info = unlockAppList.get(position);
				holder.iv_lock.setImageResource(R.drawable.ic_line_lock);
			} else {
				info = lockedAppList.get(position);
				holder.iv_lock.setImageResource(R.drawable.ic_line_unlock);
			}
			holder.iv_appicon.setImageDrawable(info.getIcon());
			holder.tv_appname.setText(info.getName());
			holder.iv_lock.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if(unlockflag) {
						Animation anim = AnimationUtils.loadAnimation(AppLockActivity.this, R.anim.translate_out_toright);
						view.startAnimation(anim);
						new Thread(new Runnable(){
							@Override
							public void run() {
								dao.add(info.getPackName());
								unlockAppList.remove(info);
								lockedAppList.add(info);
								Collections.sort(lockedAppList);
								
								SystemClock.sleep(500);
								handler.sendEmptyMessage(MSG_UNLOCK);
							}
						}).start();
					} else {
						Animation anim = AnimationUtils.loadAnimation(AppLockActivity.this, R.anim.translate_out_toleft);
						view.startAnimation(anim);
						new Thread(new Runnable(){
							@Override
							public void run() {
								dao.delete(info.getPackName());
								lockedAppList.remove(info);
								unlockAppList.add(info);
								Collections.sort(unlockAppList);
								
								SystemClock.sleep(500);
								handler.sendEmptyMessage(MSG_LOCK);
							}
						}).start();
					}
				}
			});
			
			return view;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		@Override
		public long getItemId(int position) {
			return 0;
		}
	}
	
	static class ViewHolder {
		ImageView iv_appicon;
		TextView tv_appname;
		ImageView iv_lock;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.tv_unlock:
			tv_unlock.setBackgroundResource(R.drawable.tab_left_pressed);
			tv_locked.setBackgroundResource(R.drawable.tab_right_default);
			
			handler.sendEmptyMessage(MSG_UNLOCK);
			break;
		case R.id.tv_locked:
			tv_unlock.setBackgroundResource(R.drawable.tab_left_default);
			tv_locked.setBackgroundResource(R.drawable.tab_right_pressed);

			handler.sendEmptyMessage(MSG_LOCK);
			break;
		}
	}
	
	public void setting(View v) {
		Intent intent = new Intent(this, SettingActivity.class);
		startActivity(intent);
	}
}
